
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:massagist/manager/config.dart';
import 'package:massagist/models/acctravel_model_entity.dart';
import 'package:massagist/pages/complaint_page.dart';
import 'package:massagist/pages/date_travel_page.dart';
import 'package:massagist/pages/show_image_page.dart';

import '../generated/l10n.dart';
import '../views/all_dialogs.dart';

class AcctravelInfoPage extends StatefulWidget {
  AcctravelModelEntity model;
  AcctravelInfoPage({required this.model});
  @override
  State<AcctravelInfoPage> createState() => AcctravelInfoPageState();
}

class AcctravelInfoPageState extends State<AcctravelInfoPage> {
  PageController _pageController = PageController(initialPage: 0);
  int _currentPage = 0;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar:  AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        centerTitle: true,
        backgroundColor: Colors.transparent,
        shadowColor: Colors.transparent,
        elevation: 0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios_sharp, color: Colors.black,),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.more_horiz, color: Colors.black,),
            onPressed: () {
              showAcctravelMoreDialog(context, (index) {
                print ("index = " + index.toString());
                if (index == 1) {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) =>  ComplaintPage()),
                  );
                }
              });
            },
          )
        ],
      ),
      body: Stack(
        children: [
          Column(
            children: [
              Container(
                child: Stack(
                  children: [
                    Container(
                      height: 375.sp,
                      child: PageView(
                        controller: _pageController,
                        onPageChanged: (int page) {
                          setState(() {
                            _currentPage = page;
                          });
                        },
                        children: widget.model.others.map((e) => GestureDetector(
                          child: Container(
                            child: Image.network(e, fit: BoxFit.fill,),
                          ),
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) =>  ShowImagePage(image: Image.network(e, fit: BoxFit.fill,),)),
                            );
                          },
                        )).toList(),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 350.sp),
                      height: 25.sp,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            Color(0x00F8F8F8),
                            Color(0xFFF8F8F8),
                          ],
                        ),
                      ),
                      child: buildDotIndicator(),
                    )
                  ],
                ),
              ),
              Expanded(child: buildInfo())
            ],
          ),

          GestureDetector(
            child: Container(
              width: double.infinity,
              height: 44.sp,
              margin: EdgeInsets.only(left: 16.sp, right: 16.sp, top: 610.sp),
              decoration: BoxDecoration(
                color: Color(0xFF00CA7E),
                borderRadius: BorderRadius.circular(22.sp), // 设置圆角半径
              ),
              child: Center(
                child: Text(
                  S.current.INVITE_ESCORTS,
                  style: TextStyle(color: Colors.white, fontSize: 16.sp),
                ),
              ),
            ),
            onTap: () {
              // showTechnicianBusyDialog(context);

              if (Config.isNull(Config.settingInfo.full)) {
                Navigator.push( context, MaterialPageRoute(builder: (context) =>  DateTravelPage(model: widget.model,)), );
              } else {
                showTechnicianBusyDialog(context);
              }
            },
          ),
        ],
      ),
    );


  }

  Widget buildDotIndicator() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(widget.model.others.length, (index) {
        return Container(
          width: _currentPage == index ? 10.sp : 4.sp,
          height: 4.sp,
          margin: EdgeInsets.symmetric(horizontal: 2.sp),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(2.sp),
            color: _currentPage == index ? Colors.white : Color(0x77FFFFFF),
          ),
        );
      }),
    );
  }

  Widget buildInfo() {
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(left: 12.sp, right: 12.sp, top: 12.sp),
        child: Column(
          children: [
            Container(
              height: 86.sp,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    alignment: Alignment.center,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(31.sp),
                      child: Image.network(
                        widget.model.icon,
                        width: 62.sp, height: 62.sp, fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 16.sp),
                    child: Text(
                      widget.model.name,
                      style: TextStyle(color: Color(0xFF110604), fontSize: 16.sp, fontWeight: FontWeight.w900),
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 16.sp),
              height: 100.sp,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 12.sp, top: 12.sp),
                    child: Text(
                      S.current.PERSON_INFO_TEXT,
                      style: TextStyle(color: Color(0xFF110604), fontSize: 14.sp, fontWeight: FontWeight.w900),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 16.sp, left: 32.sp, right: 12.sp),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Column(
                          children: [
                            Container(
                              child: Text(
                                S.current.SEX_TEXT,
                                style: TextStyle(color: Color(0xFF999999), fontSize: 10.sp),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 6.sp),
                              child: Text(
                                widget.model.sex,
                                style: TextStyle(color: Color(0xFF333333), fontSize: 14.sp, fontWeight: FontWeight.w500),
                              ),
                            )
                          ],
                          crossAxisAlignment: CrossAxisAlignment.center,
                        ),
                        Column(
                          children: [
                            Container(
                              child: Text(
                                S.current.AGE_TEXT,
                                style: TextStyle(color: Color(0xFF999999), fontSize: 10.sp),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 6.sp),
                              child: Text(
                                widget.model.age,
                                style: TextStyle(color: Color(0xFF333333), fontSize: 14.sp, fontWeight: FontWeight.w500),
                              ),
                            )
                          ],
                          crossAxisAlignment: CrossAxisAlignment.center,
                        ),
                        Column(
                          children: [
                            Container(
                              child: Text(
                                S.current.HEIGHT_TEXT,
                                style: TextStyle(color: Color(0xFF999999), fontSize: 10.sp),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 6.sp),
                              child: Text(
                                widget.model.height,
                                style: TextStyle(color: Color(0xFF333333), fontSize: 14.sp, fontWeight: FontWeight.w500),
                              ),
                            )
                          ],
                          crossAxisAlignment: CrossAxisAlignment.center,
                        ),
                        Column(
                          children: [
                            Container(
                              child: Text(
                                S.current.WEIGHT_TEXT,
                                style: TextStyle(color: Color(0xFF999999), fontSize: 10.sp),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 6.sp),
                              child: Text(
                                widget.model.weight,
                                style: TextStyle(color: Color(0xFF333333), fontSize: 14.sp, fontWeight: FontWeight.w500),
                              ),
                            )
                          ],
                          crossAxisAlignment: CrossAxisAlignment.center,
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(top: 16.sp),
              height: 153.sp,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 12.sp, top: 12.sp),
                    child: Text(
                      S.current.PERSON_INTRODUCE_TEXT,
                      style: TextStyle(color: Color(0xFF110604), fontSize: 14.sp, fontWeight: FontWeight.w900),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 12.sp, top: 8.sp, right: 12.sp),
                    height: 37.sp,
                    child: Text(
                      widget.model.introduce,
                      style: TextStyle(color: Color(0xFF110604), fontSize: 13.sp, fontWeight: FontWeight.w100),
                      maxLines: 2,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 12.sp, top: 12.sp),
                    child: Text(
                      S.current.DATEABLE_ATTRACTIONS,
                      style: TextStyle(color: Color(0xFF110604), fontSize: 14.sp, fontWeight: FontWeight.w900),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 12.sp, top: 8.sp, right: 12.sp),
                    child: Row(
                      children: widget.model.scenic.map((e) => Container(
                        child: Row(
                          children: [
                            Container(
                              padding: EdgeInsets.only(left: 9.sp, right: 9.sp),
                              width: 50.sp,
                              height: 20.sp,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10.sp),
                                border: Border.all(
                                  color: Color(0xFF00CA7E), width: 1.sp,
                                ),
                              ),
                              child: Center(
                                child: Text(
                                  e,
                                  style: TextStyle(color: Color(0xFF00CA7E), fontSize: 12),
                                ),
                              ),
                            ),
                            SizedBox(width: 8.sp,)
                          ],
                        ),
                      )).toList(),
                    ),
                  )
                ],
              ),
            ),
            Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 12.sp, top: 12.sp),
                    child: Text(
                      S.current.PERSON_INTRODUCE_TEXT,
                      style: TextStyle(color: Color(0xFF110604), fontSize: 14.sp, fontWeight: FontWeight.w700),
                    ),
                  ),
                  bulidIntroduceWidget()
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget bulidIntroduceWidget() {
    int rowSize = 0;
    if (widget.model.others.length % 2 == 0) {
      rowSize = widget.model.others.length ~/ 2; // 如果可以被2整除，返回value除以2
    } else {
      rowSize =  (widget.model.others.length ~/ 2) + 1; // 如果不能被2整除，返回value除以2加1
    }

    List<Widget> columnChildren = [];
    for (int i = 0; i < rowSize; i++) {
      bool hasRight = false;
      if ((i * 2 + 1) < widget.model.others.length) {
        hasRight = true;
      } else {
        hasRight = false;
      }

      columnChildren.add(
        Container(
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.only(left: 12.sp, right: 12.sp), height: 159.sp,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      alignment: Alignment.center,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8.sp),
                        child: Image.network(
                          widget.model.others[i * 2],
                          width: 159.sp,
                          height: 159.sp,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    hasRight ? Container(
                      alignment: Alignment.center,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8.sp),
                        child: Image.network(
                          widget.model.others[i * 2 + 1],
                          width: 159.sp,
                          height: 159.sp,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ) : Container()
                  ],
                ),
              ),
              SizedBox(height: 9.sp,)
            ],
          ),
        )
      );
    }

    return Container(
      margin: EdgeInsets.only(top: 12.sp),
      child: Column(
        children: columnChildren,
      ),
    );
  }
}